package com.cti.ftpservice.vo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public abstract class FtpBaseTaskInfoVo {

	/**
	 * 下载数据路径
	 */
	protected Queue<Object> downFileInfos= new LinkedList<Object>();
	
	/**
	 * 任务
	 */
	protected List<Thread> threads=new ArrayList<Thread>();
	
	/**
	 * 已经处理的书目
	 */
	protected List<Object> dealFiles=new ArrayList<Object>();
	
	/**
	 * 需要处理的项目数量
	 */
	private int totalCount;
	
	/**
	 * 已经下载书的数量
	 */
	protected int dealCount;
	
	/**
	 * 线程数
	 */
	protected int threadCount;

	

	/**
	 * 是否下载完成
	 */
	protected boolean finished;
	
	
	

	public Queue<Object> getDownFileInfos() {
		return downFileInfos;
	}

	public void setDownFileInfos(Queue<Object> downFileInfos) {
		this.downFileInfos = downFileInfos;
	}

	public List<Object> getDealFiles() {
		return dealFiles;
	}

	public void setDealFiles(List<Object> dealFiles) {
		this.dealFiles = dealFiles;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getDealCount() {
		return dealCount;
	}

	public void setDealCount(int dealCount) {
		this.dealCount = dealCount;
	}

	public boolean getFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public int getThreadCount() {
		return threadCount;
	}

	public void setThreadCount(int threadCount) {
		this.threadCount = threadCount;
	}

	/**
	 * 获取下载列表中最顶端的元素
	 * 
	 * @return
	 */
	public Object getFirstObjectItem() {
		synchronized (this) {
			if (downFileInfos == null || downFileInfos.isEmpty())
				return null;
			Object currentFileInfo = this.downFileInfos.poll();
			this.downFileInfos.remove(currentFileInfo);
			return currentFileInfo;
		}
	}
	
	/**
	 * 添加元素
	 * 
	 * @param downloadfilePath
	 */
	public void addObjectItem(Object object) {
		synchronized (this) {
			downFileInfos.offer(object);
		}
	}
	
	
	public void adddealItem(Object dealItem,boolean save) {
		synchronized (this) {
			if(save)
				dealFiles.add(dealItem);
			dealCount++;
			if(dealCount==totalCount){
				setFinished(true);
			}	
		}
	}
	
	
	/**
	 * 多线程下载
	 * @throws InterruptedException
	 * @throws NumberFormatException
	 * @throws IOException
	 */
	public abstract void startWork () throws InterruptedException, NumberFormatException, IOException;
}
